package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.modules.health.dto.HmWeightRecordDTO;
import io.renren.modules.health.service.HmWeightRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;


/**
 * 体重记录
 */
@RestController
@RequestMapping("api/hmweightrecord")
@Api(tags="体重记录")
public class HmWeightRecordController {
    @Autowired
    private HmWeightRecordService hmWeightRecordService;

    @Login
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<HashMap<String, Object>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        params.put(Constant.PAGE,"1");
        params.put(Constant.LIMIT,Integer.MAX_VALUE + "");
        params.put(Constant.ORDER,Constant.DESC);
        params.put(Constant.ORDER_FIELD,"update_date");
        if (params.get("cusId") != null)params.put("cusId", Long.parseLong((String) params.get("cusId")));
        PageData<HmWeightRecordDTO> page = hmWeightRecordService.page(params);
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("list",page.getList());
        resultData.put("total",page.getTotal());
        if (page.getTotal() > 0) {
            resultData.put("oldWeight",page.getList().get(page.getTotal() - 1).getWeight());
        }
        return new Result<HashMap<String, Object>>().ok(resultData);
    }

    @Login
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<HmWeightRecordDTO> get(@PathVariable("id") Long id){
        HmWeightRecordDTO data = hmWeightRecordService.get(id);

        return new Result<HmWeightRecordDTO>().ok(data);
    }

    @Login
    @PostMapping
    @ApiOperation("保存")
    public Result<HmWeightRecordDTO> save(@RequestBody @Validated HmWeightRecordDTO dto){
        HashMap<String, Object> param = new HashMap<>();
        param.put("updateDate", LocalDate.now());
        if (dto.getCusId() != null)param.put("cusId", dto.getCusId());
        PageData<HmWeightRecordDTO> pageData = hmWeightRecordService.page(param);
        if (pageData.getTotal() > 0) {
            hmWeightRecordService.update(pageData.getList().get(0).setWeight(dto.getWeight()));
            dto = pageData.getList().get(0);
        } else {
            hmWeightRecordService.save(dto);
        }

        return new Result<HmWeightRecordDTO>().ok(dto);
    }


    @Login
    @DeleteMapping
    @ApiOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        hmWeightRecordService.delete(ids);

        return new Result();
    }

}